/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __PONY_HAL_H__
#define __PONY_HAL_H__

/* includes (standard library, system) ---------------------------------------*/
#include <stdarg.h>
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
#include <x_list.h>
#include <x_errno.h>
#include <kal.h>
/* includes (local) ----------------------------------------------------------*/

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* defines -------------------------------------------------------------------*/

#ifndef HAL_MAX_TIMEOUT
#define HAL_MAX_TIMEOUT 0xFFFFFFFFU
#endif

/* typedefs ------------------------------------------------------------------*/

typedef struct hal_dev hal_dev_t;

typedef struct hal_dev_ops {
    int (*open)(hal_dev_t *dev, int oflag, va_list valist);
    int (*close)(hal_dev_t *dev);
} hal_dev_ops_t;

struct hal_dev {
    dlist_t node;
    const char *name;
    uint8_t open_cnt;
    uint8_t reserved[3];
    const hal_dev_ops_t *ops;
};

/* macro ---------------------------------------------------------------------*/

#define hal_dev_close(dev) hal_close(dev)

/* inlines -------------------------------------------------------------------*/
/* externs -------------------------------------------------------------------*/

hal_dev_t *hal_open(const char *name, int oflag, ...);
int hal_close(hal_dev_t *dev);
hal_dev_t *hal_find(const char *name);
int hal_register(hal_dev_t *dev);
int hal_unregister(hal_dev_t *dev);

int hal_dev_init(hal_dev_t *dev, const hal_dev_ops_t *ops);
int hal_dev_open(hal_dev_t *dev, int oflags, ...);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __PONY_HAL_H__ */
